package graphique;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.*;
import executer.Controleur;

public class FormulaireEvenement extends JFrame{

	private JLabel objetLabel, heureDebutLabel, heureFinLabel, lieuLabel, descLabel, vide, obl, typeLabel, heure1, min1, heure2, min2;
	private JTextField t_obj, t_lieu, t_desc;
	private JRadioButton rb1, rb2, rb3, rb4;
    private ButtonGroup bg = new ButtonGroup();
    private JComboBox HD, HF, MD, MF;
    private JButton valide, annuler;
    private String objE = null, HDE = null, HFE = null, MDE = null, MFE = null, lieuE = null, descE = null, typeE = "Evenement";
    private String dateEvent;
    private Controleur controleur;

	public FormulaireEvenement(String titre,String date,Controleur c){
            super(titre);
			dateEvent=date;
			controleur=c;
			this.setSize(550, 350);
			this.setLocationRelativeTo(null);
			this.setResizable(false);
			//termine le processus lorsque l'on clic sur la croix
                        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			this.initEvent();
			this.setVisible(true);
	}

    
    private void initEvent(){
setLayout(new GridLayout(1, 6));
		
		//cadre identifiant
		JPanel pan = new JPanel();
		
		pan.setBackground(Color.white);
		pan.setPreferredSize(new Dimension(500, 250)); //dimension du cadre
		
		pan.setBorder(BorderFactory.createTitledBorder("Creation d'un evenement"));
		
		t_obj = new JTextField("");
		t_obj.setPreferredSize(new Dimension(300, 25));
		objetLabel = new JLabel("Objet :");
		Box hBox1 = Box.createHorizontalBox();
        hBox1.add(objetLabel);
        hBox1.add(Box.createHorizontalStrut(5));
        hBox1.add(t_obj);
        
        typeLabel = new JLabel("Type :");
        rb1 = new JRadioButton("Evenement");
        rb2 = new JRadioButton("Anniversaire");
        rb3 = new JRadioButton("Rendez-vous");
        rb4 = new JRadioButton("Reunion");
        rb1.setSelected(true);
        rb1.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				typeE = "Evenement";				
			}
		});
		rb2.addActionListener(new ActionListener() {					
			@Override
			public void actionPerformed(ActionEvent arg0) {
				typeE = "Anniversaire";				
			}
		});
		rb3.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				typeE = "Rendez-vous";				
			}
		});
		rb4.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				typeE = "Reunion";				
			}
		});
        bg.add(rb1);
        bg.add(rb2);
        bg.add(rb3);
        bg.add(rb4);
        Box hBox2 = Box.createHorizontalBox();
        hBox2.add(typeLabel);
        hBox2.add(Box.createHorizontalStrut(5));
        hBox2.add(rb1);
        hBox2.add(Box.createHorizontalStrut(5));
        hBox2.add(rb2);
        hBox2.add(Box.createHorizontalStrut(5));
        hBox2.add(rb3);
        hBox2.add(Box.createHorizontalStrut(5));
        hBox2.add(rb4);
        
        heureDebutLabel = new JLabel("Heure de debut: ");
        heureFinLabel = new JLabel("Heure de fin: ");
        heure1 = new JLabel("H");
        min1 = new JLabel("Min");
        heure2 = new JLabel("H");
        min2 = new JLabel("Min");
        String tabHeure[] = new String[24];
        String tabMinute[] = new String[12];
		for (int i=0;i<24; i++){
				tabHeure[i]=String.valueOf(i);
		}
		int k = 0;
		for (int j=0;j<12; j++){
			tabMinute[j]=String.valueOf(k);
			k = k+5;
		}
		HD = new JComboBox(tabHeure);
		HD.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				HDE = (String) HD.getSelectedItem();
			}
		});
		HF = new JComboBox(tabHeure);
		HF.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				HFE = (String) HF.getSelectedItem();
			}
		});
		MD = new JComboBox(tabMinute);
		MD.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MDE = (String) MD.getSelectedItem();
			}
		});
		MF = new JComboBox(tabMinute);
		MF.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MFE = (String) MF.getSelectedItem();
			}
		});
		Box hBox3 = Box.createHorizontalBox();
        hBox3.add(heureDebutLabel);
        hBox3.add(Box.createHorizontalStrut(100));
        hBox3.add(HD);
        hBox3.add(Box.createHorizontalStrut(5));
        hBox3.add(heure1);
        hBox3.add(Box.createHorizontalStrut(60));
        hBox3.add(MD);
        hBox3.add(Box.createHorizontalStrut(5));
        hBox3.add(min1);
		Box hBox4 = Box.createHorizontalBox();
        hBox4.add(heureFinLabel);
        hBox4.add(Box.createHorizontalStrut(120));
        hBox4.add(HF);
        hBox4.add(Box.createHorizontalStrut(5));
        hBox4.add(heure2);
        hBox4.add(Box.createHorizontalStrut(60));
        hBox4.add(MF);
        hBox4.add(Box.createHorizontalStrut(5));
        hBox4.add(min2);
        
        t_lieu = new JTextField("");
		t_lieu.setPreferredSize(new Dimension(300, 25));
		lieuLabel = new JLabel("Lieu :");
		Box hBox5 = Box.createHorizontalBox();
        hBox5.add(lieuLabel);
        hBox5.add(Box.createHorizontalStrut(5));
        hBox5.add(t_lieu);
        
        t_desc = new JTextField("");
		t_desc.setPreferredSize(new Dimension(300, 25));
		descLabel = new JLabel("Description :");
		Box hBox6 = Box.createHorizontalBox();
        hBox6.add(descLabel);
        hBox6.add(Box.createHorizontalStrut(5));
        hBox6.add(t_desc);
		
        valide = new JButton("Creer Evenement");
        valide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				objE = t_obj.getText();
				lieuE = t_lieu.getText();
				descE = t_desc.getText();
				if(objE.isEmpty()){
					boitePwdErreur();
				}
				else {
					String[] t = dateEvent.split("\\.");
					String data[] = new String[7];
					data[0]=t[2];
					data[1]=t[1];
					data[2]=t[0];
					data[3]=typeE;
					data[4]=HDE;
					data[5]=MDE;
					data[6]=descE + " " + objE;
					controleur.ajouterEvent(data);
					boiteSucces();
					dispose();
				}
					
			}
		});
        
        Box vBox = Box.createVerticalBox();
        vBox.add(hBox1);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox2);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox3);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox4);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox5);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox6);
        pan.add(vBox);
        
        annuler = new JButton("Annuler");
        annuler.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent event){
    			dispose();
    		}
    	});
        
        obl = new JLabel("* champs obligatoire");
        obl.setFont(new Font("ARIAL",Font.ITALIC,10));
		
		JPanel content1 = new JPanel();
		content1.setBackground(Color.white);
		content1.add(pan);
		content1.add(valide);
		content1.add(annuler);
		content1.add(obl);
		add(content1, BorderLayout.NORTH);
    }

    private void boitePwdErreur()
    {
        JOptionPane.showMessageDialog(null, 
                                      "Erreur dans la saisie de l'evenement",
                                      "Erreur", 
                                      JOptionPane.ERROR_MESSAGE);
    }
    
    private void boiteSucces()
    {
        JOptionPane.showMessageDialog(null, 
                                      "Ajout de l'evenement reussi",
                                      "Information", 
                                      JOptionPane.INFORMATION_MESSAGE);
    }

    public String getObjetE(){
    	return objE;
    }
    
    public String getTypeE(){
    	return typeE;
    }
    
    public String getHDE(){
    	return HDE;
    }
    
    public String getHFE(){
    	return HFE;
    }
    
    public String getMDE(){
    	return MDE;
    }
    
    public String getMFE(){
    	return MFE;
    }
    
    public String getLieuE(){
    	return lieuE;
    }

    public String getDescriptionE(){
    	return descE;
    }
}

